<?php
/**
 * Created by PhpStorm.
 * User: 93195
 * Date: 2018/2/24
 * Time: 13:43
 */
class point {
    /**
     * 我的积分首页
     */
    public function index()
    {
        $cid = Session::$user['customerId'];
        $userId= Session::$user['userId'];
        Session::switchCustomer(0);
        $pointModel = M("module_user_point");
        Session::switchCustomer($cid);
        $result = $pointModel->where("userId = $userId and cid = $cid")->find();
        if (empty($result)) $result = array();
        Output::instance()->module('point', 'index')->object($result);
    }

    /**
     * 我的积分明细列表
     */
    public function userPointBill($start, $limit)
    {
        $cid = Session::$user['customerId'];
        $userId= Session::$user['userId'];
        Session::switchCustomer(0);
        $pointBillModel = M("module_user_point_bill");
        Session::switchCustomer($cid);
        $result = $pointBillModel->where("userId = $userId and cid = $cid")->order('createTime desc')->limit($start, $limit)->select();
        $count = $pointBillModel->where("userId = $userId and cid = $cid")->select();
        $count = count($count);
        if (empty($result)) $result = array();
        Output::instance()->module('point', 'list')->object([
            'code' => 0,
            'data' => $result,
            'total' => $count
        ]);
    }

    /**手机端获取积分商品
     * @param bool $ispoint
     * @param string $name
     * @param string $isRemoved
     * @param string $brand
     * @param string $supplierName
     * @param string $categoryName
     * @param int $start
     * @param int $limit
     * @param string $order
     * @param string $orderBy
     * @param bool $isExport
     * @param null $isAll
     * @param null $cId
     * @param null $serialNumber
     * @param null $vendor
     * @param null $tagName
     * @param int $fromPoints
     * @param null $supportSpecification
     */
    public function getPointProduct($ispoint=false, $name='',$isRemoved='', $brand='',$supplierName='',$categoryName='', $start = 0, $limit = 25,
                                    $order = 'prod.number', $orderBy = 'desc', $isExport = false,$isAll=null,$cId=null,$serialNumber=null,$vendor=null,$tagName=null, $fromPoints = 1, $supportSpecification = null)
    {
        $cid = Session::$user['customerId'];
        Session::switchCustomer(0);
            $settingModel = M('customer_setting');
        Session::switchCustomer($cid);

        $pointPic = $settingModel->where("cid = $cid")->getField('pointPic');
        $product = R('Product/getAllProduct', array($ispoint, $name,$isRemoved, $brand, $supplierName, $categoryName, $start, $limit,
            $order, $orderBy, $isExport, $isAll, $cId, $serialNumber, $vendor, $tagName, $fromPoints, $supportSpecification, 1));
        Output::instance()->module('point', 'exchange')->object([
            'code' => 0,
            'data' => [
                'productList' => $product,
                'pointPic' => $pointPic
            ]
        ]);

    }

    public static function pointSuccess($oid)
    {
        $cid = Session::$user['customerId'];
        Session::switchCustomer(0);
        $settingModel = M('module_product_order_detail');
        Session::switchCustomer($cid);

        $name = $settingModel->where(['oid' => $oid])->getField('name');
        Output::instance()->module('point', 'pointSuccess')->object([
            'code' => 0,
            'proName' => $name
        ]);
    }
}